#include"ListWidgetWorker.h" 

#include<QDir>
#include<stdio.h>

#include"Song.h"


ListWidgetWorker::ListWidgetWorker(){
	
}

ListWidgetWorker::~ListWidgetWorker(){
	
}

void ListWidgetWorker::run(){
	printf("ListWidgetWorker:: started\n");
	while( !m_urlsListList.isEmpty() ){
		QList<QUrl> urls = m_urlsListList.takeAt(0);
		
		for(int i = 0; i < urls.size(); i++){
			QString file(urls[i].toString());
			if(file.contains("file://"))
				file.remove("file://");
			QFileInfo fi(file);
			if(fi.isFile())
				droppedFile(file);
			else if(fi.isDir())
				droppedDir(file);
			else
				printf("%s is neither file nor directory :|\n", file.toUtf8().data());
		}
	}
}

void ListWidgetWorker::droppedFile(QString path){
// 	printf("Dropped file: %s\n", path.toUtf8().data());
	try{
		QPointer<Song> song = new Song(path);
		emit addSong(song);
	}catch(...){
		
	}
}

void ListWidgetWorker::droppedDir(QString path){
// 	printf("Dropped dir: %s\n", path.toAscii().data());
	QDir dir(path);
	if(!dir.exists())
		return;
	for(unsigned i = 0; i < dir.count(); i++){
		if(dir[i]=="."||dir[i]=="..")
			continue;
// 		printf("Processing %s\n", dir[i].toAscii().data());
			
		QFileInfo fi(dir.absoluteFilePath(dir[i]));
		if(fi.isFile()){
			droppedFile(dir.absoluteFilePath(dir[i]));
		}else if(fi.isDir()){
			droppedDir(dir.absoluteFilePath(dir[i]));
		}
	}
}
